﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;

namespace ACC.Extensions.PCClient.Helper
{
    public static class LanguageHelper
    {
        public static void SelectChinese()
        {
            LoadLanguageFile("pack://application:,,,/Resources/Custom/Lang/zh-cn.xaml");
        }

        public static void SelectEnglish()
        {
            LoadLanguageFile("pack://application:,,,/Resources/Custom/Lang/en-us.xaml");
        }

        public static void SetLanguage(string langShort)
        {
            string resourcePath = "pack://application:,,,/Resources/Custom/Lang/{0}.xaml";
            switch (langShort.ToLower())
            {
                case "cn":
                    resourcePath = String.Format(resourcePath, "zh-cn");
                    break;
                case "en":
                    resourcePath = String.Format(resourcePath, "en-us");
                    break;
                default:
                    resourcePath = String.Format(resourcePath, "zh-cn");
                    break;
            }
            LoadLanguageFile(resourcePath);
        }

        private static void LoadLanguageFile(string fileName)
        {
            Application.Current.Resources.MergedDictionaries[0] = new ResourceDictionary()
            {
                Source = new Uri(fileName, UriKind.RelativeOrAbsolute)
            };
        }

        public static string LoadStringFromResource(string itemName)
        {
            ResourceDictionary resource = Application.Current.Resources.MergedDictionaries[0];
            if (resource != null)
            {
                var item = resource[itemName];
                if (item != null)
                {
                    return item.ToString();
                }
            }
            return "";
        }
    }
}
